package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.ProductSkuBo;
import com.ruoyi.supplier.domain.vo.ProductSkuVo;
import com.ruoyi.supplier.domain.ProductSku;
import com.ruoyi.supplier.mapper.ProductSkuMapper;
import com.ruoyi.supplier.service.IProductSkuService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品规格Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class ProductSkuServiceImpl implements IProductSkuService {

    private final ProductSkuMapper baseMapper;

    /**
     * 查询商品规格
     */
    @Override
    public ProductSkuVo queryById(Integer productSkuId){
        return baseMapper.selectVoById(productSkuId);
    }

    /**
     * 查询商品规格列表
     */
    @Override
    public TableDataInfo<ProductSkuVo> queryPageList(ProductSkuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductSku> lqw = buildQueryWrapper(bo);
        Page<ProductSkuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品规格列表
     */
    @Override
    public List<ProductSkuVo> queryList(ProductSkuBo bo) {
        LambdaQueryWrapper<ProductSku> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProductSku> buildQueryWrapper(ProductSkuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProductSku> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, ProductSku::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getSpecSkuId()), ProductSku::getSpecSkuId, bo.getSpecSkuId());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNo()), ProductSku::getProductNo, bo.getProductNo());
        lqw.eq(bo.getProductPrice() != null, ProductSku::getProductPrice, bo.getProductPrice());
        lqw.eq(bo.getLinePrice() != null, ProductSku::getLinePrice, bo.getLinePrice());
        lqw.eq(bo.getLowPrice() != null, ProductSku::getLowPrice, bo.getLowPrice());
        lqw.eq(bo.getStockNum() != null, ProductSku::getStockNum, bo.getStockNum());
        lqw.eq(StringUtils.isNotBlank(bo.getProductWeight()), ProductSku::getProductWeight, bo.getProductWeight());
        lqw.eq(bo.getMerchantId() != null, ProductSku::getMerchantId, bo.getMerchantId());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuAttrs()), ProductSku::getSkuAttrs, bo.getSkuAttrs());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), ProductSku::getImage, bo.getImage());
        lqw.eq(bo.getImageType() != null, ProductSku::getImageType, bo.getImageType());
        lqw.eq(StringUtils.isNotBlank(bo.getVideo()), ProductSku::getVideo, bo.getVideo());
        lqw.eq(bo.getStockId() != null, ProductSku::getStockId, bo.getStockId());
        lqw.eq(bo.getStatus() != null, ProductSku::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增商品规格
     */
    @Override
    public Boolean insertByBo(ProductSkuBo bo) {
        ProductSku add = BeanUtil.toBean(bo, ProductSku.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProductSkuId(add.getProductSkuId());
        }
        return flag;
    }

    /**
     * 修改商品规格
     */
    @Override
    public Boolean updateByBo(ProductSkuBo bo) {
        ProductSku update = BeanUtil.toBean(bo, ProductSku.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProductSku entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品规格
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean save(ProductSkuBo skuBo) {
        if(skuBo.getProductSkuId() == null){
            return this.insertByBo(skuBo);
        }else{
            return this.updateByBo(skuBo);
        }
    }

    @Override
    public Boolean deleteByProductId(Integer productId) {
        QueryWrapper<ProductSku> queryWrapper = Wrappers.query();
        queryWrapper.lambda().eq(ProductSku::getProductId,productId);
        return baseMapper.delete(queryWrapper) > 0;
    }
}
